package com.example.notepaddemo;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Created by 谢汉荣 on 2018/6/10.
 */

public class TopBar extends RelativeLayout {
    private Button leftButton,rightBUtton;
    private TextView titleTextVIew;
    private OnLeftAndRightClickListener listener;

    public void setOnLeftAndRightClickListener(OnLeftAndRightClickListener listener) {
        this.listener = listener;
    }
    public interface OnLeftAndRightClickListener{
        void OnLeftButtonClick();
        void OnRightButtonClick();
    }
    public void setLeftButtonVisibility(boolean flag){
        if (flag)
            leftButton.setVisibility(View.VISIBLE);
        else
            leftButton.setVisibility(View.GONE);
    }
    public void setRightButtonVisibility(boolean flag) {
        if (flag) {
            rightBUtton.setVisibility(View.VISIBLE);
        }
        else {
            rightBUtton.setVisibility(View.GONE);
        }
    }
    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.layout_topbar,this);
        leftButton = findViewById(R.id.leftButton);
        rightBUtton = findViewById(R.id.rightButton);
        titleTextVIew = findViewById(R.id.titleText);
        leftButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.OnLeftButtonClick();
                }
            }
        });
        rightBUtton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.OnRightButtonClick();
                }
            }
        });
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.TopBar);
        int leftBtnBackground = typedArray.getResourceId(R.styleable.TopBar_leftBackground,0);
        int rightBtnBackground = typedArray.getResourceId(R.styleable.TopBar_rightBackground, 0);
        String titleText = typedArray.getString(R.styleable.TopBar_titleText);
        float titleTextSize = typedArray.getDimension(R.styleable.TopBar_titleTextSize,0);
        int titleTextColor = typedArray.getColor(R.styleable.TopBar_titleTextColor,0x38ad5a);

        typedArray.recycle();
        leftButton.setBackgroundResource(leftBtnBackground);
        rightBUtton.setBackgroundResource(rightBtnBackground);
        titleTextVIew.setText(titleText);
        titleTextVIew.setTextSize(titleTextSize);
        titleTextVIew.setTextColor(titleTextColor);
    }
}
